import { dirname} from "node:path"
import { fileURLToPath } from "node:url"
import { VueLoaderPlugin } from "vue-loader"
import FileManagerPlugin from 'filemanager-webpack-plugin'
import path from 'path'
import AutoImport from 'unplugin-auto-import/webpack'
import Components from 'unplugin-vue-components/webpack'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import CopyPlugin from 'copy-webpack-plugin'

// npm run build:card --card=text
const getCurrentDir = () => {
  const __filename = fileURLToPath(import.meta.url)
  const __dirname = dirname(__filename)
  return __dirname
}
const __dirname = getCurrentDir()

const cardName = process.env.npm_config_card
const cardJson = await import(`../src/cards/${cardName}/card.json`,{ assert: {type: 'json'} })
const version = cardJson.default.version || '1.0.0'
const outputDir = `${cardName}-${version}`
export default {
  mode: "production",
  entry: {
    index: path.resolve(__dirname, `../src/cards/${cardName}/index.js`),
    panel: path.resolve(__dirname, `../src/cards/${cardName}/panel/index.js`)
  },
  output: {
    filename: "[name].js",
    path:  path.resolve(__dirname, `./dist`),
    library: { type: 'module' }
    // library: "MyPlugin",
    // libraryTarget: "umd",
  },
  experiments: { outputModule: true },
  plugins: [
    // 引入插件
    new VueLoaderPlugin(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    new CopyPlugin({
      patterns: [
        { from: path.resolve(__dirname, `../src/cards/${cardName}/card.json`), to: path.resolve(__dirname, `./dist`) },
      ],
    }),
    new FileManagerPlugin({
      events: {
        onEnd: {
          delete: [
           path.resolve(__dirname, `./.zip/${outputDir}.zip`),
          ],
          archive: [
           {source: path.resolve(__dirname, `./dist`), destination: path.resolve(__dirname, `../.zip/${outputDir}.zip`)},
          ]
         }
      }
     })
  ],
  externals: {
    "vue": 'https://6c6f-lowcode-2gxh0baw4d19d595-1256670128.tcb.qcloud.la/assets/vue.esm.js'
    // vue: 'https://cdn.jsdelivr.net/npm/vue@3.2.37/dist/vue.esm-browser.js'
    // 'vue': 'http://localhost:8000/vue.esm-browser.js',
    // 'echarts': 'https://cdn.jsdelivr.net/npm/echarts@5.1.2/+esm',
  },
  optimization: {
    minimize: true,
    // 拆分代码
    // splitChunks:{
    //   chunks:'all',
    // }
  },
  module: {
    rules: [
      // vue
      {
        test: /\.vue/,
        use: ["vue-loader"],
      },
      // JS
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              // 开始
              [
                "@babel/preset-env",
                {
                  useBuiltIns: "usage",
                  corejs: {
                    version: 3,
                  }
                },
              ],
              // 结束
            ],
          },
        },
      },
      // CSS
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      // Images
      {
        test: /\.(jpg|png|gif|svg)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "assets/images/",
            },
          },
        ],
      },

      // Fonts
      {
        test: /\.(ttf|eot|woff|woff2)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "assets/fonts/",
            },
          },
        ]
      }
    ]
  }
}